/**
 * Search - wiele wyszukiwarek ..
 *
 * @author		Marcin `widmogrod` Habryn, (widmogrod@gmail.com)
 * @version		0.0.1
 * @license		GNU GPL
 */
var Search = {
	blockInfo: 'search-info',
	blockForm: 'search-form',
	searchInput: 'search-input',
	initialize: function(){
		// ustaw domyslne opcje
		$('#' + Search.blockInfo).html(Search.description('google'));
		$('#' + Search.blockForm).attr('action',Search.searchUrl('google',''));
	
		// przechwytuj wyslanie zdarzenia przez enter
		$('#' + Search.blockForm).submit(function(){
			var search = $('#' + Search.searchInput).attr('value');
	
			if(search == undefined) return false;
	
			var url = $(this).attr('action')+search;
			
			location.href = url;
			return false;
		});
	
		// opisy wyszukiwarek
		$('input[class~=search][type=submit]').mouseover(function(){
			var info = '';
			var name = $(this).attr('class');

			$('#' + Search.blockInfo).html(Search.description(name));
			$('#' + Search.blockForm).attr('action',Search.searchUrl(name,''));
		});
	
		// zdarzenie po kliknieciu przycisku wyszukiwania
		$('input[class~=search][type=submit]').click(function(){
			var search = $('#' + Search.searchInput).attr('value');
	
			if(search == undefined) return;
	
			var url = (url = Search.searchUrl($(this).attr('class'),search)) === false
				? Search.searchUrl('search-google',search)
				: url;
	
			// otworz w nowym oknie
			if(false){
				window.open(url);
			} else {
				location.href = url;
			}
		});
	},
	description: function(name){
		var result = null;
		switch(name){
	 		case 'pwn':
			case 'search-pwn':
				result = 'Pwn.pl - encyklopedia';
				break;
			case 'ceneo':
			case 'search-ceneo':
				result = 'Ceneo.pl - porównanie cen';
				break;
			case 'google':
			case 'search-google':
				result = 'Google.pl - wyszukiwarka stron WWW';
				break;
			case 'allegro':
			case 'search-allegro':
				result = 'Allegro.pl - aukcje internetowe';
				break;
			case 'wikipedia':
			case 'search-wikipedia':
				result = 'pl.Wikipedia.org - wolna encyklopedia';
				break;
			case 'slownik-online':
			case 'search-slownik-online':
				result = 'Slownik-online.pl - słownik wyrazów obcych';
				break;
		}

		return '<strong>Szukwaj w:</strong> ' + result;
	},
	searchUrl: function(name,search){
		switch(name){
	 		case 'pwn':
			case 'search-pwn':
				return 'http://encyklopedia.pwn.pl/lista.php?co='+search;
			case 'ceneo':
			case 'search-ceneo':
				return 'http://www.ceneo.pl/categories.aspx?search=yes&categoryID=0&searchText=' + search + '&inDesc=False&minPrice=0&maxPrice=99999999';
			case 'google':
			case 'search-google':
				return 'http://www.google.pl/search?hl=pl&q=' + search;
			case 'allegro':
			case 'search-allegro':
				return 'http://www.allegro.pl/search.php?string=' + search;
			case 'wikipedia':
			case 'search-wikipedia':
				return 'http://pl.wikipedia.org/wiki/Specjalna:Szukaj?search=' + search;
			case 'slownik-online':
			case 'search-slownik-online':
				return 'http://www.slownik-online.pl/cgibin/search?charset=utf-8&words=' + search;
			default:
				return false;
		}
	}
};